package com.bluesky.android.utils;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.widget.BaseAdapter;

public class BatchImageDownloader extends AsyncTask<Void, Void, Void> {

    
    ImageDownloaderTask[] downloadTask = new ImageDownloaderTask[10];
    BaseAdapter adapter;
    HashMap<String, Drawable> imageCache;
    private static int position = 0;
    
    public BatchImageDownloader(BaseAdapter adapter,
            HashMap<String, Drawable> imageCache) {
        this.adapter = adapter;
        this.imageCache = imageCache;
        for(int i = 0; i< downloadTask.length; i++){
        	 downloadTask[i] = new ImageDownloaderTask();
        }
    }

    public void addUrl(String url) {
        if(position == downloadTask.length){
        	position = 0;
        }
        downloadTask[position].addUrl(url);
        position = position + 1;
    }
    @Override
    protected void onCancelled(){
    	for(int i = 0; i< downloadTask.length; i++){
       	 	downloadTask[i].stop();			
       }
    }
    
    @Override
    protected Void doInBackground(Void... params) {
        for(ImageDownloaderTask task: downloadTask){
        	task.start();
        }        
        return null;
    }

    @Override
    protected void onProgressUpdate(Void... values) {    	
    		adapter.notifyDataSetChanged();    	
    }

    @Override
    protected void onPostExecute(Void result) {
        adapter.notifyDataSetChanged();
    }

    public Drawable downloadImage(String url) {

        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpGet request = new HttpGet(url);
        try {
            HttpResponse response = httpClient.execute(request);
            InputStream stream = response.getEntity().getContent();
            Drawable drawable = Drawable.createFromStream(stream, "src");
            
            return drawable;
        } catch (ClientProtocolException e) {
            e.printStackTrace();
            return null;
        } catch (IllegalStateException e) {
            e.printStackTrace();
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }

    }
    private class ImageDownloaderTask extends Thread{
    	List<String> imgUrls = new ArrayList<String>();
    	 public void addUrl(String url) {
    		 imgUrls.add(url);
    	}
    	public void run(){
    		for (String url : imgUrls) {
    			
    				if (!imageCache.containsKey(url)) {
    					Drawable bm = downloadImage(url);
                    if (null != bm) {                    	
                    		imageCache.put(url, bm);
                    		publishProgress();                    	
                    }
               
    		}
    	}
    }
  }

}